﻿using AVCUMPlatform.DAL.Interface;
using AVCUMPlatform.HelperLibrary;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVCUMPlatform.DAL.Implement
{
    /// <summary>
    /// 数据访问基础类
    /// </summary>
    public partial class BasDAL<T>
    {
        public bool BaseAdd(object obj)
        {
            string sql = SQLHelper.BuildInsertSQL<T>();
            return DapperHelper.ExecuteCommand(sql, obj);
        }
        /// <summary>
        /// 添加并返回自增id
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int BaseAddReturnAutoId(object obj)
        {
            string sql = SQLHelper.BuildInsertSQL<T>(returnAutoId: true);
            return DapperHelper.GetValue<int>(sql, obj);
        }

        public bool BaseDelete(List<int> ids)
        {
            if (ids == null || !ids.Any())
            {
                return false;
            }
            string sql = SQLHelper.BuildBatchDeleteSql<T>(ids);
            return DapperHelper.ExecuteCommand(sql);
        }

        public bool BaseUpdate(object obj)
        {
            string sql = SQLHelper.BuildUpdateSQL<T>();
            return DapperHelper.ExecuteCommand(sql, obj);
        }

        public T BaseGet(int id)
        {
            string sql = SQLHelper.BuildSelectSQL<T>(null, new string[] { "id" });
            return DapperHelper.GetModel<T>(sql, new { id });
        }

        public List<T> BaseGetList(out int count, int page = 1, int per_page = 20)
        {
            string sql = SQLHelper.BuildSelectSQL<T>();
            sql = sql.Substring(0, sql.Length - 1);

            string countsql = "select count(*) from (" + sql + ") as temp;";
            count = DapperHelper.GetValue<int>(countsql);

            sql += " limit @sindex,@eindex;";
            return DapperHelper.GetModelList<T>(sql, new { sindex = (page * per_page) - per_page, eindex = per_page });
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public int GetCount(string whereStr = null)
        {
            string sql = SQLHelper.BuildGetCountSql<T>(whereStr);
            return DapperHelper.GetValue<int>(sql);
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public T GetModel(string whereStr = null)
        {
            string sql = SQLHelper.BuildGetByWhereSql<T>(whereStr);
            return DapperHelper.GetModel<T>(sql);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public List<T> GetModelList(string whereStr = null)
        {
            string sql = SQLHelper.BuildGetByWhereSql<T>(whereStr, limit1: false);
            return DapperHelper.GetModelList<T>(sql);
        }

        /// <summary>
        /// 单表分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="param"></param>
        /// <param name="count"></param>
        /// <param name="where"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public List<T> SingleTablePageQuery(int pageIndex, int pageSize, out int count, string orderByFieldName, bool isDesc, string where, object param)
        {
            string querySql = SQLHelper.BuildSingleTablePageQuerySql<T>(where, orderByFieldName, isDesc, pageIndex, pageSize);
            List<T> tList = DapperHelper.GetModelList<T>(querySql, param);
            string countSql = SQLHelper.BuildSingleTableCoutSql<T>(where);
            count = DapperHelper.GetValue<int>(countSql, param);
            return tList;
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="tList"></param>
        public void RealBatchInsert(List<T> tList)
        {
            Util.BatchProcess(tList, 1000, new Action<List<T>>(tempList =>
            {
                string sql = SQLHelper.BuildRealBatchInsertSql(tempList);
                DapperHelper.ExecuteCommand(sql);
            }));
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="tList"></param>
        public void RealBatchUpdate(List<T> tList)
        {
            Util.BatchProcess(tList, 1000, new Action<List<T>>(tempList =>
            {
                string sql = SQLHelper.BuildBatchUpdateSql(tempList);
                DapperHelper.ExecuteCommand(sql);
            }));
        }
    }
}
